#region File Description
//-----------------------------------------------------------------------------
// FPS.cs
//
// Copyright (c) VosSoft 2009
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace VosSoft.Xna
{
    /// <summary>
    /// FPS counter.
    /// </summary>
    public class FPS : DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        SpriteFont font;

        Vector2 position;
        Color color1, color2;

        int fps = 0, frameCounter = 0;
        double elapsedTimeFPS = 0.0d;

        public FPS(Game game)
            : this(game, new Vector2(10.0f), Color.White, Color.Black)
        {
        }

        public FPS(Game game, Vector2 position, Color color1, Color color2)
            : base(game)
        {
            this.position = position;
            this.color1 = color1;
            this.color2 = color2;
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            font = Game.Content.Load<SpriteFont>("font");

            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            frameCounter++;
            elapsedTimeFPS += gameTime.ElapsedGameTime.TotalSeconds;
            if (elapsedTimeFPS >= 1.0d)
            {
                fps = frameCounter;
                frameCounter = 0;
                elapsedTimeFPS = 0.0d;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.DrawString(font, fps.ToString(), position, color1);
            spriteBatch.DrawString(font, fps.ToString(), position - new Vector2(1.0f), color2);
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
